package com.anxin.act.task.addTask;


import java.util.List;

import com.anxin.act.utils.ActUtils;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.impl.bpmn.behavior.*;
import org.activiti.engine.impl.el.FixedValue;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;

import org.activiti.engine.impl.task.TaskDefinition;

//会签节点创造者
public class MultiInstanceActivityCreator extends RuntimeActivityCreatorSupport implements RuntimeActivityCreator {

	public ActivityImpl[] createActivities(ProcessEngine processEngine, ProcessDefinitionEntity processDefinition,RuntimeActivityDefinitionEntity info,String type) {
		info.setFactoryName(MultiInstanceActivityCreator.class.getName());
		RuntimeActivityDefinitionEntityIntepreter radei = new RuntimeActivityDefinitionEntityIntepreter(info);

		if (radei.getCloneActivityId() == null) {
			//获取新的activityId
			String cloneActivityId = createUniqueActivityId(info.getProcessInstanceId(), radei.getPrototypeActivityId(),type);
			radei.setCloneActivityId(cloneActivityId);
		}

		return new ActivityImpl[] { this.createMultiInstanceActivity(processEngine, processDefinition, info.getProcessInstanceId(),radei.getPrototypeActivityId(), radei.getCloneActivityId(), radei.getSequential(), radei.getAssignees()) };
	}


	private ActivityImpl createMultiInstanceActivity(ProcessEngine processEngine, ProcessDefinitionEntity processDefinition,
			String processInstanceId, String prototypeActivityId, String cloneActivityId, boolean isSequential, List<String> assignees) {


		ActivityImpl prototypeActivity = ActUtils.getActivityImpl(processDefinition.getId(), prototypeActivityId,false);

//		TaskDefinition taskDefinition = (TaskDefinition) prototypeActivity.getProperties().get("taskDefinition");
//		taskDefinition.setKey(cloneActivityId);
//		taskDefinition.setAssigneeExpression(new FixedValue("法务负责人"));




		//拷贝listener，executionListeners会激活历史记录的保存
		ActivityImpl clone = cloneActivity(processDefinition, prototypeActivity, cloneActivityId, "executionListeners", "properties");
		//拷贝所有后向链接
		for (PvmTransition trans : prototypeActivity.getOutgoingTransitions()) {
			clone.createOutgoingTransition(trans.getId()).setDestination((ActivityImpl) trans.getDestination());
		}

		MultiInstanceActivityBehavior multiInstanceBehavior = isSequential ?
				new SequentialMultiInstanceBehavior(clone,(TaskActivityBehavior) prototypeActivity.getActivityBehavior())
				: new ParallelMultiInstanceBehavior(clone,(TaskActivityBehavior) prototypeActivity.getActivityBehavior());

		clone.setActivityBehavior(multiInstanceBehavior);

		clone.setScope(true);
		clone.setProperty("multiInstance", isSequential ? "sequential" : "parallel");

		//设置多实例节点属性
		multiInstanceBehavior.setLoopCardinalityExpression(new FixedValue(assignees.size()));

		TaskDefinition taskDefinition = cloneTaskDefinition((TaskDefinition) clone.getProperties().get("taskDefinition"));
		taskDefinition.setKey(cloneActivityId);
//		taskDefinition.setAssigneeExpression(new FixedValue("法务负责人"));
//		taskDefinition.setCandidateGroupIdExpressions(null);
//		taskDefinition.setCandidateUserIdExpressions(null);
		clone.getProperties().put("taskDefinition",taskDefinition);

		return clone;
	}
}
